package com.na.tools.ping.deprecated;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.na.tools.ping.NetworkChecker;

/**
 * @see NetworkChecker
 */
@Deprecated
public class PingCheck2 implements Comparable<PingCheck2> {

	// step 1. go to https://cloudmonitor.ca.com/zh_cn/ping.php, and ping your website
	// step 2. copy the table's content, paste to ping.txt
	// step 3. run the program
	public static void main(String[] args) throws IOException {
		InputStream txt = PingCheck2.class.getResourceAsStream("ping.txt");
		List<PingCheck2> results = parseResult(txt);
		for (PingCheck2 ping : results) {
			ping.connect();
		}
		System.out.println();
		System.out.println();
		Collections.sort(results);
		for (PingCheck2 ping : results) {
			System.out.println(ping);
		}
	}
	
	public String checkPoint;
	public String result; // Okay or others...
	public float minRtt;
	public float avgRtt;
	public float maxRtt;
	public String ip;
	public long connectTime;
	
	public long connect() {
		try {
			System.out.println("try connect " + ip);
			long time = System.currentTimeMillis();
			Socket socket = new Socket();
			socket.connect(new InetSocketAddress(ip, 80), 3000);
			time = System.currentTimeMillis() - time;
			connectTime = time;
			socket.close();
		} catch (Exception e) {
			connectTime = Long.MAX_VALUE;
		}
		return connectTime;
	}
	
	@Override
	public int compareTo(PingCheck2 o) {
		// order by connectTime asc
		if (connectTime > o.connectTime) return 1;
		if (connectTime < o.connectTime) return -1;
		return 0;
	}
	
	@Override
	public String toString() {
		return "ip:" + ip + ", connect:" + connectTime + " millis";
	}
	
	protected static List<PingCheck2> parseResult(InputStream in) {
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
			String line;
			List<PingCheck2> results = new ArrayList<PingCheck2>();
			while ((line = reader.readLine()) != null) {
				results.add(parseLine(line));
			}
			return results;
		} catch (IOException e) {
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
				}
			}
		}
		return null;
	}
	
	protected static PingCheck2 parseLine(String line) {
		String[] strs = line.split("\t");
		PingCheck2 ping = new PingCheck2();
		if (strs.length > 0) ping.checkPoint = strs[0];
		if (strs.length > 1) ping.result = strs[1];
		if (strs.length > 2 && strNotEmpty(strs[2])) ping.minRtt = Float.parseFloat(strs[2]);
		if (strs.length > 3 && strNotEmpty(strs[3])) ping.avgRtt = Float.parseFloat(strs[3]);
		if (strs.length > 4 && strNotEmpty(strs[4])) ping.maxRtt = Float.parseFloat(strs[4]);
		if (strs.length > 5) ping.ip = strs[5];
		return ping;
	}
	
	private static boolean strNotEmpty(String str) {
		return str != null && str.length() > 0;
	}
	
}
